﻿//-----------------------------------------------------------------------
// <copyright file="DocumentViewerScrollToBottom.cs" company="AFS">
// Copyright (c) AFS. All rights reserved.
//
//    This file is part of Chatbox.
//
//    Chatbox is free software: you can redistribute it and/or modify
//    it under the terms of the GNU Lesser General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    any later version.
//
//    Chatbox is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//    GNU Lesser General Public License for more details.
//
//    You should have received a copy of the GNU Lesser General Public License
//    along with Chatbox. If not, see http://www.gnu.org/copyleft/lesser.html
// </copyright>
//----------------------------------------------------------------------- 

namespace ChatBox.UI
{
    using System.Windows.Controls;
    using System.Windows.Threading;

    using ChatBox.UI.Controls;

    /// <summary>
    /// Class to scroll a document viewer to bottom
    /// </summary>
    public class DocumentViewerScrollToBottom 
    {
        /// <summary>
        /// The document viewer to work on
        /// </summary>
        private readonly FlowDocumentScrollViewer flowDocumentScrollViewer;

        /// <summary>
        /// Was the document at the bottom at last test
        /// </summary>
        private bool markWasAtBottom;

        /// <summary>
        /// Initializes a new instance of the DocumentViewerScrollToBottom class
        /// </summary>
        /// <param name="flowDocumentScrollViewer">the document viewer to work on</param>
        public DocumentViewerScrollToBottom(FlowDocumentScrollViewer flowDocumentScrollViewer)
        {
            this.flowDocumentScrollViewer = flowDocumentScrollViewer;
        }

        /// <summary>
        /// Set a mark of whether the scrollviewer was at the bottom
        /// </summary>
        public void MarkAtBottom()
        {
            ScrollViewer scrollViewer = this.GetScrollViewer();
            if (scrollViewer == null)
            {
                this.markWasAtBottom = true;
            }
            else
            {
                this.markWasAtBottom = scrollViewer.IsAtBottom();
            }
        }

        /// <summary>
        /// Move back to the bottom if it was there
        /// </summary>
        public void ResetAtBottom()
        {
            if (this.markWasAtBottom)
            {
                // move the document viewer back to the bottom
                // but do it on the main thread, only after the doc has completely loaded
                this.flowDocumentScrollViewer.Dispatcher.Invoke(
                    DispatcherPriority.ApplicationIdle,
                    new NoArgEventHandler(this.DoScrollToBottom));
            }
        }

        /// <summary>
        /// Do the Scrolling to bottom
        /// </summary>
        private void DoScrollToBottom()
        {
            ScrollViewer scrollViewer = this.GetScrollViewer();
            if (scrollViewer != null)
            {
                scrollViewer.ScrollToBottom();
            }
        }

        /// <summary>
        /// Get the scroll viewer in the FlowDocumentScrollViewer
        /// </summary>
        /// <returns>The scroll viewer in the flowDocumentScrollViewer</returns>
        private ScrollViewer GetScrollViewer()
        {
            return this.flowDocumentScrollViewer.FindScrollViewer();
        }
    }
}
